package com.longteng.diamond.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.longteng.diamond.dao.ProjectMapper;
import com.longteng.diamond.domain.InterfaceTest;
import com.longteng.diamond.domain.Project;
import com.longteng.diamond.domain.User;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.service.ProjectService;
import com.longteng.diamond.service.UserService;
import com.longteng.diamond.util.SessionUtil;
import com.longteng.diamond.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator
 * DATE : 2018/7/1
 * TIME : 14:09
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Controller
@Slf4j
@RequestMapping("/projectController")
public class ProjectController {

    @Autowired
    UserService userService;
    @Autowired
    ProjectService projectService;
    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    ValidUtil validUtil;
    /**
     * 跳转项目页面
     * @param
     * @return view
     *
     */
    @RequestMapping("/projectPage")
    public String projectPage() {
        return "page/project/projectPage";
    }
    /**
     * 获取项目列表
     * @param project
     * @return TablePage
     *
     */
    @RequestMapping("/getProject")
    public @ResponseBody
    TablePage getProject(Project project) {
        TablePage tablePage = new TablePage();
        try {
            tablePage = projectService.getProject(project);
        } catch (Exception e) {
            log.error("获取用户错误:", e);
        }
        return tablePage;
    }
    /**
     * 删除项目校验,是否存在数据
     * @param projectId
     * @return RequestResult
     */
    @RequestMapping("/deleteProjectCheck")
    public @ResponseBody RequestResult deleteProjectCheck(Integer projectId){
        RequestResult requestResult=projectService.deleteProjectCheck(projectId);
        return requestResult;
    }
    /**
     *通过id删除项目
     * @param projectId
     * @return RequestResult
     */
    @RequestMapping("/deleteProjectById")
    public @ResponseBody RequestResult deleteProjectById(Integer projectId){
        RequestResult requestResult=new RequestResult();
        try {
            projectService.deleteProjectById(projectId);
            requestResult.setSuccess(true);
        }catch (Exception e){
            log.error("删除项目异常",e);
            requestResult.setSuccess(false);
            requestResult.setMsg("删除项目异常,请联系管理员!");
        }
        return requestResult;
    }
    /**
     * 编辑项目页面
     * @param project
     * @return ModelAndView
     */
    @RequestMapping("/editProjectPage")
    public ModelAndView editProjectPage(Project project) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/project/editProjectPage");
        if (null != project.getProjectId() && project.getProjectId() > 0) {
            project = projectMapper.findProjectById(project.getProjectId());
        }
        modelAndView.addObject("project", project);
        return modelAndView;
    }
    /**
     * 页面选择项目调用的操作，更新用户的lastProjectId
     * @param projectId
     * @param httpSession
     * @return RequestResult
     */
    @RequestMapping("/selectProject")
    public String selectProject(Integer projectId, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        /**设置session用户最后一次访问的项目**/
        user.setLastProjectId(projectId);
        /**同时更新数据库user的lastProjectId**/
        userService.updateUser(user);
        /**重定向到index页面**/
        return "redirect:/index";
    }
    /**
     * 获取项目的header
     * @param projectId
     * @return RequestResult
     */
    @RequestMapping("/getHeaders")
    public @ResponseBody
    RequestResult getHeaders(Integer projectId) {
        RequestResult requestResult = new RequestResult();
        try {
            Project project = projectMapper.findProjectById(projectId);
            String headerStr = project.getHeaders();
            if(StringUtils.isEmpty(headerStr)){
                requestResult.setSuccess(false);
                requestResult.setMsg("您的项目里面没有headers,请先到项目管理创建headers");
                return requestResult;
            }
            Map map=JSON.parseObject(headerStr,Map.class);
            requestResult.setObj(map);
            requestResult.setSuccess(true);
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("获取header错误了,请联系管理员!");
            log.error("获取项目错误异常", e);
        }
        return requestResult;
    }
    /**
     * 新增项目
     * @param project
     * @return ModelAndView
     */
    @RequestMapping("/addProject")
    public ModelAndView addInterfaceTest(@Valid Project project, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(project.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/project/editProjectPage");
            return modelAndView;
        }
        try {
            projectService.addProject(project);
        } catch (Exception e) {
            log.error("新增项目错误", e);
            modelAndView.setViewName("page/project/editProjectPage");
            bindingResult.rejectValue("projectName", "0", "新增项目错误,请联系管理员!");
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/projectController/projectPage");
        return modelAndView;
    }

    /**
     * 更新项目
     * @param project
     * @return ModelAndView
     */
    @RequestMapping("/updateProject")
    public ModelAndView updateInterfaceTest(@Valid Project project, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(project.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/project/editProjectPage");
            return modelAndView;
        }
        try {
            projectMapper.updateProject(project);
        } catch (Exception e) {
            log.error("更新接口错误", e);
        }
        modelAndView.setViewName("redirect:/projectController/projectPage");
        return modelAndView;
    }
}
